package personal.jinhui.paprika.ui;

import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

import personal.jinhui.paprika.PaprikaService;
import personal.jinhui.paprika.service.Contact;
import personal.jinhui.paprika.service.PresenceAdapter;
import personal.jinhui.paprika.utils.PaprikaBroadcastReceiver;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import personal.jinhui.paprika.R;
import personal.jinhui.paprika.service.aidl.IXmppFacade;

/**
 * This activity is used to accept a subscription request.
 */
public class SubscriptionActivity extends Activity {
	private static final String TAG = "SubscriptionActivity";

	private static final Intent SERVICE_INTENT = new Intent();
	static {
		SERVICE_INTENT.setComponent(new ComponentName(
				"personal.jinhui.paprika",
				"personal.jinhui.paprika.PaprikaService"));
	}

	private IXmppFacade mService;
	private Contact mContact;
	private ServiceConnection mServConn = new PaprikaServiceConnection();
	private final PaprikaBroadcastReceiver mReceiver = new PaprikaBroadcastReceiver();
	private MyOnClickListener mClickListener = new MyOnClickListener();

	/**
	 * Constructor.
	 */
	public SubscriptionActivity() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subscription);
		findViewById(R.id.SubscriptionAccept)
				.setOnClickListener(mClickListener);
		findViewById(R.id.SubscriptionRefuse)
				.setOnClickListener(mClickListener);
		mContact = new Contact(getIntent().getData());
		TextView tv = (TextView) findViewById(R.id.SubscriptionText);
		String str = String.format(getString(R.string.SubscriptText), mContact.getJID());
		tv.setText(str);
		this.registerReceiver(mReceiver, new IntentFilter(
				PaprikaBroadcastReceiver.Paprika_CONNECTION_CLOSED));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		bindService(new Intent(this, PaprikaService.class), mServConn,
				BIND_AUTO_CREATE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		unbindService(mServConn);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(mReceiver);
	}

	/**
	 * Send the presence stanza.
	 * 
	 * @param p
	 *            presence stanza
	 */
	private void sendPresence(Presence p) {
		PresenceAdapter preAdapt = new PresenceAdapter(p);
		try {
			mService.sendPresencePacket(preAdapt);
		} catch (RemoteException e) {
			Log.e(TAG, "Error while sending subscription response", e);
		}
	}

	/**
	 * Event simple click on buttons.
	 */
	private class MyOnClickListener implements OnClickListener {

		/**
		 * Constructor.
		 */
		public MyOnClickListener() {
		}

		@Override
		public void onClick(View v) {
			Presence presence = null;
			switch (v.getId()) {
			case R.id.SubscriptionAccept:
				presence = new Presence(Type.subscribed);
				Toast.makeText(SubscriptionActivity.this,
						getString(R.string.SubscriptAccept), Toast.LENGTH_SHORT)
						.show();
				break;
			case R.id.SubscriptionRefuse:
				presence = new Presence(Type.unsubscribed);
				Toast.makeText(SubscriptionActivity.this,
						getString(R.string.SubscriptRefused),
						Toast.LENGTH_SHORT).show();
				break;
			default:
				Toast.makeText(SubscriptionActivity.this,
						getString(R.string.SubscriptError), Toast.LENGTH_SHORT)
						.show();
			}
			if (presence != null) {
				presence.setTo(mContact.getJID());
				sendPresence(presence);
			}
			finish();
		}
	};

	/**
	 * The ServiceConnection used to connect to the Paprika service.
	 */
	private class PaprikaServiceConnection implements ServiceConnection {

		/**
		 * Constructor.
		 */
		public PaprikaServiceConnection() {
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mService = IXmppFacade.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}
	}
}
